#include<linux/module.h>
#include<linux/sched.h>
#include<linux/list.h>
#include<linux/kthread.h>
MODULE_LICENSE("GPL");
int my_function(void *argc)
{
	printk("in the kernel thread function!\n");
	printk("the current pid is:%d\n",current->pid);
	printk("out the kernel thread function\n");
	return 0;
}
static int __init add_wait_queue_init(void)
{
	char namefrm[]= "add_wait_queue.c";
	char namefrm1[]="add_wait_queue1.c";
	struct task_struct *result,*result1;
	int wait_queue_num=0;
	wait_queue_head_t head;
	wait_queue_t data,data1,*curr,*next;
	printk("into add_wait_queue_init.\n");

	result=kthread_create_on_node(my_function,NULL,-1,namefrm);
	result1=kthread_create_on_node(my_function,NULL,-1,namefrm1);

	init_waitqueue_head(&head);
	
	init_waitqueue_entry(&data,result);
	init_waitqueue_entry(&data1,result1);

	add_wait_queue(&head,&data);
	add_wait_queue(&head,&data1);
	__wake_up(&head,TASK_ALL,0,NULL);

	list_for_each_entry_safe(curr,next,&(head.task_list),task_list)
	{
		wait_queue_num++;	

		printk("the flag value of the current data of the waitqueue is:%d\n",curr->flags);

		printk("the pid value os the current data of the waitqueue is:%d\n",
				((struct task_struct*)(curr->private))->pid);
	}
	printk("the value of the wait_queue_num is:%d\n",wait_queue_num);

	printk("the pid of result is :%d\n",result->pid);
	printk("the pid of result1 is :%d\n",result1->pid);
	printk("the current pid is:%d\n",current->pid);
	return 0;
}
static void __exit add_wait_queue_exit(void)
{
	printk("Goodbye add_wait_queue\n");
}
module_init(add_wait_queue_init);
module_exit(add_wait_queue_exit);
